package cn.mysdp.biz.facade;


import cn.mysdp.biz.convert.*;
import cn.mysdp.biz.domain.*;
import cn.mysdp.biz.dto.request.*;
import cn.mysdp.biz.dto.response.*;
import cn.mysdp.biz.repository.*;
import cn.mysdp.biz.facade.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: SdpProjectFacadeBaseImpl
 * @Description:
 * @Author: SDP
 * @Date: 2021-10-30
 * @Version: 1.0
 * Table: sdp_project
 * Comment:
 * 
 */
public class SdpProjectFacadeBaseImpl extends BaseServiceImpl implements SdpProjectFacade {
    @Autowired
    protected SdpProjectMapper sdpProjectMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        map.put("unique_project_index_7", "workspace_name-组名-name");
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addSdpProject(SdpProjectAddRequest request) throws Exception {
        SdpProjectWithBLOBs daoRequest = new SdpProjectAddRequestConvert().convert(request);
        fixEqualCondition(request, "sdp_project", "add", "", null);
        Integer result = 0;
        try {
            result = sdpProjectMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "sdp_project", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateSdpProject(SdpProjectUpdateRequest request) throws Exception {
        fixEqualCondition(request, "sdp_project", "update", request.getId()+"", null);
        checkInternalId(request, "sdp_project", "update", request.getId());
        addHistory(request, "U", "sdp_project", "api", request.getId(), null);

        SdpProjectWithBLOBs daoRequest = new SdpProjectUpdateRequestConvert().convert(request);
        SdpProjectExample example = new SdpProjectExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), SdpProjectExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = sdpProjectMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteSdpProject(SdpProjectDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "sdp_project", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "sdp_project", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "sdp_project", "api", request.getIdList().get(index), null);
            sdpProjectMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public SdpProjectQueryResponse querySdpProject(SdpProjectQueryRequest request) throws Exception {
        SdpProjectWithBLOBs daoResponse = sdpProjectMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        SdpProjectQueryResponse response = new SdpProjectQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<SdpProjectQueryResponse> listSdpProject(SdpProjectQueryRequest request) throws Exception {
        SdpProjectWithBLOBs daoRequest = new SdpProjectWithBLOBs();
        copyProperties(request, daoRequest);
        SdpProjectExample example;
        try {
            example = SdpProjectExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<SdpProjectWithBLOBs> list;
        try {
            list = sdpProjectMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<SdpProjectQueryResponse> responseList = new ArrayList<>();
        for(SdpProjectWithBLOBs item : list) {
            SdpProjectQueryResponse newItem = new SdpProjectQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countSdpProject(SdpProjectQueryRequest request) throws Exception {
        SdpProjectWithBLOBs daoRequest = new SdpProjectWithBLOBs();
        copyProperties(request, daoRequest);
        SdpProjectExample example;
        try {
            example = SdpProjectExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = sdpProjectMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statSdpProject(SdpProjectQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public SdpProjectForExecuteResponse execute(SdpProjectForExecuteRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public SdpProjectForShellResponse shell(SdpProjectForShellRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public Integer clone(BaseNameIdRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
